//
//  PencilTool.m
//  iPadDev01
//
//  Created by smu on 11-7-28.
//  Copyright 2011年 duk. All rights reserved.
//

#import "PencilTool.h"
#import "PathDrawingInfo.h"
#import "SynthesizeSingleton.h"  //提供一种标准化的方法来让所有的类都变成单例类
//因为对于每一个工具，我们只需要一个实例


@implementation PencilTool
@synthesize delegate;
SYNTHESIZE_SINGLETON_FOR_CLASS(PencilTool);

- (id)init {
    if ((self = [super init])) {
        trackingTouches = [[NSMutableArray array] retain];
        startPoints = [[NSMutableArray array] retain];
        paths = [[NSMutableArray array] retain];
    }
        return self;
}

- (void)activate{}

- (void)deactivate 
{
    [trackingTouches removeAllObjects];
    [startPoints removeAllObjects];
    [paths removeAllObjects];
}


//一个或更多的手指触摸下在萤幕上,它发送了touchesBegan ： withEvent ：讯息
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (UITouch *touch in [event allTouches]) {
        //remember the touch, and its original start point, for future
        [trackingTouches addObject:touch];
        CGPoint location = [touch locationInView:touchedView];
        [startPoints addObject:[NSValue valueWithCGPoint:location]];
        //创建新UIBezierPath对象
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineCapStyle = kCGLineCapButt;
        [path moveToPoint:location];  //设置起点
        [path setLineWidth:delegate.strokeWidth];
        [path addLineToPoint:location];  //为要添加到形状的每个增加的连接线使用addLineToPoint
        [paths addObject:path];
    }
}    
        
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
 
//一个或更多的手指从萤幕上离开，它发送了touchesEnded ： withEvent ：讯息。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in [event allTouches]) {
        // make a line from the start point to the current point  
        NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
        // only if we actually remember the start of this touch...
        if (touchIndex != NSNotFound) {
            UIBezierPath *path = [paths objectAtIndex:touchIndex];
            PathDrawingInfo *info = [PathDrawingInfo pathDrawingInfoWithPath:path fillColor:[UIColor clearColor] strokeColor:delegate.strokeColor];
            [delegate addDrawable:info];
            [trackingTouches removeObjectAtIndex:touchIndex];
            [startPoints removeObjectAtIndex:touchIndex];
            [paths removeObjectAtIndex:touchIndex];
        }
    }
}

//一个或更多的手指移动，它发送了touchesMoved ： withEvent ：讯息。  
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIView *touchedView = [delegate viewForUseWithTool:self];
    for (UITouch *touch in [event allTouches]) {
        // make a line from the start point to the current point
        NSUInteger touchIndex = [trackingTouches indexOfObject:touch];
        // only if we actually remember the start of this touch ...
        if (touchIndex != NSNotFound) {
            CGPoint location = [touch locationInView:touchedView];
            UIBezierPath *path = [paths objectAtIndex:touchIndex];
            [path addLineToPoint:location];
        }
    }
}
 
- (void)drawTemporary
{
    for (UIBezierPath *path in paths) {
        //对象渲染到当前的图形上下文
        [delegate.strokeColor setStroke];
        [path stroke];
    }
}
        
@end
